<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    function myPromiseRace(promiseArr) {
     // 如果传入的参数不是可迭代类型
     if (promiseArr[Symbol.iterator] === undefined) {
        return Promise.reject("params is not a iteratorable type")
    }

    // 转为数组进行遍历
    promiseArr = Array.from(promiseArr);

    return new Promise((resolve, reject) => {
        for(let promise of promiseArr) {
            promise.then(res=>{
                resolve(res);
            }).catch(err=>{
                reject(err);
            })
        }
    })
}
const pArr = [
    Promise.resolve(1),
    new Promise(resolve=>{
        setTimeout(()=>{
            resolve(2);
        },200)
    }),
    Promise.resolve(3),
]

const pArr_1 = [
    new Promise(resolve=>{
        setTimeout(()=>{
            resolve(1);
        },200)
    }),
    Promise.reject(2),
]

myPromiseRace(pArr).then(res=>{
    console.log(res);
}).catch(err=>{
    console.log(err);
})
// 1

myPromiseRace(pArr_1).then(res=>{
    console.log(res);
}).catch(err=>{
    console.log(err);
})
// 2

myPromiseRace(Promise.resolve(1)).then(res=>console.log(res)).catch(err=>console.log(err));
// params is not a iteratorable type
  </script>
</body>
</html>